package com.example.democrud.common.file.excel.generate;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.WriteWorkbook;

import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName ExcelGenerator
 * @Author Administrator
 * @Date 2020/4/27   16:36
 */
public class ExcelGenerator {
    public void write(OutputStream outputStream) {
        ExcelWriterBuilder excelWriterBuilder = new ExcelWriterBuilder();
        WriteWorkbook writeWorkbook = new WriteWorkbook();
        writeWorkbook.setOutputStream(outputStream);
        writeWorkbook.setExcelType(ExcelTypeEnum.XLSX);
        ExcelWriter excelWriter = new ExcelWriter(writeWorkbook);
        WriteSheet writeSheet = new WriteSheet();
        writeSheet.setSheetName("demo");
        WriteTable writeTable = new WriteTable();
        writeTable.setTableNo(1);
        List<List<String>> heads = new ArrayList<>();
        heads.add(Arrays.asList("id"));
        heads.add(Arrays.asList("姓名"));
        heads.add(Arrays.asList("年龄"));
        heads.add(Arrays.asList("生日"));
        writeSheet.setHead(heads);

        List<List<String>> userList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            userList.add(Arrays.asList("ID_" + i, "小明" + i, String.valueOf(i), LocalDateTime.now().toString()));
        }
        excelWriter.write(userList,writeSheet);
        excelWriter.finish();
    }
}
